package client.comm.commlib.comm_ui.draw

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import androidx.core.view.marginLeft
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

class SpacePblGridItemDecoration : RecyclerView.ItemDecoration {
    private var space: Int
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var topAndOneSpace = false

    constructor(space: Int, parent: RecyclerView) {
        this.space = space
        mPaint.style = Paint.Style.FILL
        mPaint.color = Color.parseColor("#00000000")
    }

    constructor(space: Int, topAndOneSpace: Boolean, parent: RecyclerView) {
        this.space = space
        mPaint.style = Paint.Style.FILL
        mPaint.color = Color.parseColor("#00000000")
        this.topAndOneSpace = topAndOneSpace
    }

    override fun getItemOffsets(
        outRect: Rect, view: View,
        parent: RecyclerView, state: RecyclerView.State
    ) {
        if (topAndOneSpace) {
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.left = 0
                outRect.right = 0
                outRect.top = 0
                outRect.bottom = 0
            } else {

                val layoutParams = view.getLayoutParams() as StaggeredGridLayoutManager.LayoutParams
                val spanIndex = layoutParams.getSpanIndex();

                if (spanIndex % 2 != 0) {
                    outRect.left = (space / 2f).toInt()
                    outRect.right = space
                } else {
                    outRect.left = space
                    outRect.right = (space / 2f).toInt()
                }
                outRect.top = space
                outRect.bottom = 0
            }
        } else {

            val layoutParams = view.getLayoutParams() as StaggeredGridLayoutManager.LayoutParams
            val spanIndex = layoutParams.getSpanIndex();
            if (spanIndex % 2 != 0) {
                outRect.left = (space / 2f).toInt()
                outRect.right = space
            } else {
                outRect.left = space
                outRect.right = (space / 2f).toInt()
            }
            outRect.top = space
            outRect.bottom = 0
        }
    }

    //    @Override
    //    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    //        super.onDraw(c, parent, state);
    //        drawVertical(c, parent);
    //        drawHorizontal(c, parent);
    //    }
    //绘制横向 item 分割线
    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop
        val bottom = parent.measuredHeight - parent.paddingBottom
        val childSize = parent.childCount
        for (i in 1 until childSize) {
            val child = parent.getChildAt(i)
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams
            val left = child.right + layoutParams.rightMargin
            val right = left + space
            canvas.drawRect(
                left.toFloat(),
                top.toFloat(),
                right.toFloat(),
                bottom.toFloat(),
                mPaint
            )
        }
    }

    //绘制纵向 item 分割线
    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft
        val right = parent.measuredWidth - parent.paddingRight
        val childSize = parent.childCount
        for (i in 1 until childSize) {
            val child = parent.getChildAt(i)
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + layoutParams.bottomMargin
            val bottom = top + space
            canvas.drawRect(
                left.toFloat(),
                top.toFloat(),
                right.toFloat(),
                bottom.toFloat(),
                mPaint
            )
        }
    }
}